package com.zhentao.service.impl;

import com.alibaba.nls.client.AccessToken;
import com.zhentao.aliyun.AliyunConstans;
import com.zhentao.aliyun.SpeechTranscriberWithMicrophoneDemo;
import com.zhentao.service.SpeechService;
import com.zhentao.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @ClassName: SpeechServiceImpl
 * @Author: 振涛教育_Q
 * @Date: 2024/12/4 14:00
 */
@Service
@Slf4j
public class SpeechServiceImpl implements SpeechService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private SpeechTranscriberWithMicrophoneDemo transcriber;

    /**
     * @description 开启语音转换文字
     * @author 振涛教育_小Q
     * @date    2024/12/4 14:04
     */
    @Override
    @Async // 异步执行
    public void speech() {
        try {
            log.info("语音识别已经开启了................");
            String accessKeyId = "LTAI5tCXs8pXu2EV74o7N7Dm";
            String accessKeySecret = "we14fAOTPwKWlj3ETnTtbu3pWvGjDn";
            AccessToken accessToken = new AccessToken(accessKeyId,accessKeySecret);
            accessToken.apply();
            String token = accessToken.getToken();
            long expireTime = accessToken.getExpireTime();
            transcriber =
                    new SpeechTranscriberWithMicrophoneDemo
                            ("cOqWjYlf6Q1iH3O5", token,redisTemplate);
            transcriber.process();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void shutdown() {
        transcriber.shutdown();
    }

}
